package com.kiln.juxingqs.fragment

import android.view.View
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.base.BaseFragment
import client.global.commlib.base.PagerFooterAdapter
import client.global.commlib.network.data.PagingJsonResult
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.PagingXyzrAdapter
import com.kiln.juxingqs.databinding.FragmentLoadmorepageBinding
import com.kiln.juxingqs.ui.vm.XyzrViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class ZrMxFragment : BaseFragment<FragmentLoadmorepageBinding, XyzrViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_loadmorepage
    }

    var isLoadMore = false
    val adapter: PagingXyzrAdapter by lazy {
        PagingXyzrAdapter().apply {
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }
            appendError = {
                isLoadMore = true
            }
        }
    }

    override fun initViewModel(): XyzrViewModel = createViewModel()

    override fun initWidget() {
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
        val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter.withLoadStateFooter(PagerFooterAdapter())
        binding.recyclerView.onLoadMore = {
            if (isLoadMore)
                this@ZrMxFragment.adapter.retry()
            isLoadMore = false
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
    }

    override fun initData() {
        viewModel.viewModelScope.launch {
            viewModel.pagingDataAuto(20, hold = {
                val ret = viewModel.server.zcjl(it, 10, "", "")
                PagingJsonResult(
                    list = ret.body()?.data?.record_list,
                    code = ret.body()?.code,
                    message = ret.body()?.message
                )
            }).collect {
                adapter.submitData(it)
            }
        }

        viewModel.cancleRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("已撤销")
                    adapter.refresh()
                } else {
                    toast(it.message)
                }
            }
        })
    }


}